﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using FindIt.DAL;
using FindIt.BLL;
using MySql.Data.MySqlClient;
using System.Collections;

namespace FindIt.UI.Pages
{
    public partial class WebPageResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string query = Request.QueryString["q"];
            string queryType = Request.QueryString["t"];

            Regex regex = new Regex(@"\s+");
            string[] queryParts = regex.Split(query);

            for (int i = 0; i < queryParts.Length; i++)
            {
                queryParts[i] = queryParts[i].Trim(' ', '?', '\"', ',', '\'', ';', ':', '.', '(', ')').ToLower();
                PorterStemmer stemmer = new PorterStemmer();
                queryParts[i] = stemmer.stemTerm(queryParts[i]);
            }

            PerformSearch(queryParts);
        }

        private void PerformSearch(string[] queryParts)
        {
            ArrayList list = WebPage.PerformSearch(queryParts);
            gvResults.DataSource = list;
            gvResults.DataBind();
        }

        protected void gvResults_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                Response.Redirect("WebPageView.aspx?q=" + e.CommandArgument);
            }
        }
    }
}